package com.polymorphic.polyarry;

/**
 * @Author 小闽
 * @Date 2024/8/13 9:44
 * @PackageName: com.polymorphic.polyarry
 * @ClassName: PloyArray
 * @Description: 多态数组
 * @Version 1.0
 */
public class PloyArray {
    public static void main(String[] args) {
        /**
         * 现有一个继承结果如下：要求创建1个Person对象
         * 2个Student对象和2个Teacher对象，统一放在数组中，并调用每个对象say方法
         */
         Person[] person = new Person[5];
         person[0] = new Person("jack", 20);
         person[1] = new Student("tom",21,100);
         person[2] = new Student("sun",23,90);
         person[3] = new Teacher("black", 25, 30000);
         person[4] = new Teacher("smith", 26, 10000);

        for (int i = 0; i < person.length; i++) {
            System.out.println(person[i].say()); //动态绑定机制
            if (person[i] instanceof Student){
                ((Student) person[i]).study(); // 这里使用多态的向下转型
            }else if(person[i] instanceof Teacher){
                 ((Teacher) person[i]).teach();
            }else {
                System.out.println("你的类型有误，请自己检查...");
            }
        }

    }
}
